package com.ecode.imecode;

import com.ecode.imecode.proto.Message;
import com.ecode.imecode.utils.MessageDecodeUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;

import java.util.List;

/**
 * 对于websocket的数据帧 需要使用BinaryWebSocketFrame包装
 *
 * 对websocket 数据帧进行解码 编码
 */
public class WebSocketMessageDecode extends MessageToMessageDecoder<BinaryWebSocketFrame> {
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, BinaryWebSocketFrame binaryWebSocketFrame,
                          List<Object> list) throws Exception {
        ByteBuf content = binaryWebSocketFrame.content();
        if(content.readableBytes()<28)
            return;
        Message message = MessageDecodeUtils.getMessage(content);
        if(message==null) return;
        list.add(message);
    }
}
